﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Common.MultiLanguage
{
    public class Tool
    {
        public static IList<Dictionary> RecnikCeo;

        public static void Inicijalizuj(int jezikId)
        {
            RecnikCeo = UcitajPrevode(jezikId);
        }

        public static IList<Dictionary> UcitajPrevode (int jezikId)
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                return (from t in dc.Recniks
                        where t.JezikId == jezikId
                        select new Dictionary { Original = t.Original, Prevod = t.Prevod }).ToList();
            }
        }

        #region Prevedi meni
        private static void PrevediStavkuMenija(ToolStripMenuItem m)
        {
            try
            {
                m.Text = (from t in RecnikCeo
                         where t.Original == m.Text
                         select t).SingleOrDefault().Prevod;
            }
            catch
            {
                //m.Text = String.Format("!{0}!", m.Name);
            }

            foreach (ToolStripItem t in m.DropDownItems)
            {
                if (t is ToolStripMenuItem)
                    PrevediStavkuMenija((ToolStripMenuItem)t);
            }
        }

        public static void PrevediMeni(MenuStrip menu)
        {
            foreach (ToolStripMenuItem m in menu.Items)
                PrevediStavkuMenija(m);
        }
        #endregion


        #region Prevedi formu
        public static void PrevediFormu(Form forma)
        {
            string sForma = forma.GetType().ToString();
            string[] dForma = sForma.Split(new char[] { '.' });

            foreach (Control c in forma.Controls)
            {

                if (c.GetType() == typeof(MenuStrip)) PrevediMeni((MenuStrip)c);
            }

            PrevediKontroleNaFormi(forma.Controls);
        }

        public static void PrevediKontroleNaFormi(Control.ControlCollection controlCollection)
        {
            foreach (Control c in controlCollection)
            {
                try
                {
                    c.Text = (from t in RecnikCeo
                              where t.Original == c.Text
                              select t).SingleOrDefault().Prevod;
                }
                catch
                {
                    //c.Text = String.Format("!{0}!", c.Name);
                }

                if (c.HasChildren) PrevediKontroleNaFormi(c.Controls);
            }
        }
        #endregion

    }
}
